package com.zzl.boot.bigevent.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzl.boot.bigevent.mapper.articleMapper;
import com.zzl.boot.bigevent.pojo.PageBean;
import com.zzl.boot.bigevent.pojo.article;
import com.zzl.boot.bigevent.service.articleService;
import com.zzl.boot.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class implArticleService implements articleService {
    @Autowired
    private articleMapper articleMapper;
    private Map<String,Object> map;
    @Override
    public void addArticle(article article) {
        map=ThreadLocalUtil.get();
        article.setUserId((Integer) map.get("id"));
        article.setVisitCount(0);
        articleMapper.addArticle(article);
    }

    @Override
    public PageBean<article> getClassPages(Integer pageNum, Integer pageSize, Integer id, String state) {
        //用PageHelper开启分页查询,pageNum表示第几页,pageSize表示每页的条数
        PageHelper.startPage(pageNum,pageSize);
        map=ThreadLocalUtil.get();
        List<article>  items=articleMapper.getClassPages((Integer)map.get("id"),id,state);
        //使用mybatis内置对象Page来分别得到总条数和文章集合
        Page<article> p=(Page<article>)items;
        //System.out.println(p);
        //System.out.println(p.getResult());
        return new PageBean<>(p.getTotal(),p.getResult());
    }

    @Override
    public article getById(Integer id) {
        return articleMapper.getById(id);
    }

    @Override
    public void update(article article) {
        articleMapper.update(article);
    }

    @Override
    public void del(Integer id) {
        articleMapper.delById(id);
    }

    @Override
    public void delByCate(Integer categoryId) {
        map=ThreadLocalUtil.get();
        articleMapper.delByCate(categoryId,(Integer)map.get("id"));
    }

    @Override
    public Integer getArcId() {
        map=ThreadLocalUtil.get();
        return articleMapper.getArcId((Integer)map.get("id"));
    }

    @Override
    public List<article> listByUser( ) {
        map=ThreadLocalUtil.get();
        return articleMapper.listByUser((Integer)map.get("id"));
    }

    @Override
    public PageBean<article> getList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<article>  items=articleMapper.getList();
        //使用mybatis内置对象Page来分别得到总条数和文章集合
        Page<article> p=(Page<article>)items;
        //System.out.println(p);
        //System.out.println(p.getResult());
        return new PageBean<>(p.getTotal(),p.getResult());
    }

    @Override
    public Integer getCategoryCount(Integer id) {
        return articleMapper.getCategoryCount(id);
    }

    @Override
    public List<article> listByCategory(Integer categoryId) {
        return articleMapper.listByCategory(categoryId);
    }

    @Override
    public void addView(Integer id) {
        articleMapper.addView(id);
    }

    @Override
    public List<article> search(String keyword) {
        keyword=keyword.trim();
        keyword="%"+keyword+"%";
        return  articleMapper.search(keyword);
    }

    @Override
    public String findUser(Integer id) {
       return articleMapper.findUser(id);
    }


}
